package com.herui.ota.service.iml;

import com.herui.ota.dao.mysql.DeviceBluNumbMapper;
import com.herui.ota.entity.DeviceBluNumb;
import com.herui.ota.service.DeviceBluNumbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * author name: amanda.shan
 * create time: 2024-01-15 13:40:48
 */ 
@Service
public class DeviceBluNumbServiceIml extends BaseService<DeviceBluNumbMapper, DeviceBluNumb> implements DeviceBluNumbService{

	private DeviceBluNumb deviceBluNumb;

	private AtomicInteger atomicInteger = new AtomicInteger();

	@Autowired
	@Override
	public void setBaseMapper(DeviceBluNumbMapper baseMapper) {
		super.setBaseMapper(baseMapper);
	}

	@PostConstruct
	public void init(){
		List<DeviceBluNumb> list = list();
		if (list.size() == 0){
			DeviceBluNumb bluNumb = new DeviceBluNumb();
			bluNumb.setBluNameNumb(1000);
			save(bluNumb);
			list = list();
			deviceBluNumb = list.get(0);
			atomicInteger.set(deviceBluNumb.getBluNameNumb());
		}else{
			deviceBluNumb = list.get(0);
			atomicInteger.set(deviceBluNumb.getBluNameNumb());
		}

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				int i = atomicInteger.get();
				deviceBluNumb.setBluNameNumb(i);
				updateById(deviceBluNumb);
			}
		}, 0, 10000);
	}


	@Override
	public Integer newBlueName() {

		int i = atomicInteger.incrementAndGet();

		return i;
	}
}

